package com.wt;

import android.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.content.Context;
import android.os.Bundle;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.widget.TimePicker;
import android.widget.TimePicker.ValidationCallback;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
import android.app.Activity;
import android.content.DialogInterface;
import android.widget.Button;

import android.net.Uri;
import android.media.Ringtone;
import android.media.RingtoneManager;

import java.util.HashSet;
import java.util.Calendar;

import com.android.deskclock.R;
import com.wt.DrawerLayoutV;
import com.android.deskclock.provider.Alarm;
import com.android.deskclock.AlarmClockFragment;
import com.wt.WtInterfaces.OnRingtoneChangedListener;
import com.android.deskclock.Utils;
import com.wt.WtDialog;
	
public class WtAlarmEditDialog extends AlertDialog implements OnRingtoneChangedListener{
	private TimePicker mTimePicker;
	//private final OnTimeSetListener mTimeSetCallback;

	private final int mInitialHourOfDay;
	private final int mInitialMinute;
	private final boolean mIs24HourView;
	private final Alarm mAlarm;
	private boolean mVibrateOn;
	private Uri mRingtone;
	private boolean[] mCheckedItems;
	private String[] mWeekDays;

	private DrawerLayoutV mDrawerLayoutV;
	private ImageView mActionView;
	private TextView mTitleView;
	private ImageView mVibrateCheckBox;
	private TextView mRingtoneView;

	private AlertDialog mRepeatDialog;
	private View mOtherView;
	private ViewGroup mRepeatDaysView;
	private Button[] mDayButtons = new Button[7];
	private ImageView mRepeatCheckBox;
	private boolean mRepeatOn;
	private LayoutInflater mLayoutInflater;

	private final int[] DAY_ORDER = new int[] {
		Calendar.SUNDAY,
		Calendar.MONDAY,
		Calendar.TUESDAY,
		Calendar.WEDNESDAY,
		Calendar.THURSDAY,
		Calendar.FRIDAY,
		Calendar.SATURDAY,
	};
	
	public WtAlarmEditDialog(Context context, int theme, OnAlarmEditListener callBack, int hourOfDay,int minute, boolean is24HourView, Alarm alarm) {
        	super(context, theme);
			
		mOnAlarmEditListener = callBack;
		mInitialHourOfDay = hourOfDay;
		mInitialMinute = minute;
		mIs24HourView = is24HourView;
		mAlarm = alarm;
		mWeekDays = Utils.getShortWeekdays();
		mCheckedItems = new boolean[mWeekDays.length];
		if(mAlarm != null){
			mVibrateOn = mAlarm.vibrate;
			mRingtone = mAlarm.alert;
			HashSet<Integer> setDays = mAlarm.daysOfWeek.getSetDays();
			for (int i = 0; i < 7; i++) {
				if (setDays.contains(DAY_ORDER[i])) {
					mCheckedItems[i] = true;
					mRepeatOn = true;
				}
			}
		}

		mLayoutInflater = LayoutInflater.from(getContext());
		View customView = mLayoutInflater.inflate(R.layout.wt_alarm_edit_fragment, null, false);
		setView(customView);

		setButton2(getContext().getText(android.R.string.cancel),mDialogInterfaceListener);
		setButton(getContext().getText(com.android.internal.R.string.ime_action_next),mDialogInterfaceListener);

		setTitle(getContext().getText(R.string.set_alarm));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//setView(R.layout.wt_alarm_edit_fragment);
		getButton(DialogInterface.BUTTON_NEGATIVE).setText(getContext().getText(android.R.string.cancel));
		getButton(DialogInterface.BUTTON_POSITIVE).setText(getContext().getText(com.android.internal.R.string.ime_action_next));
		getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(mOnClickListener);
		
		mTimePicker = (TimePicker) findViewById(R.id.time_picker);
		mTimePicker.setIs24HourView(mIs24HourView);
		mTimePicker.setCurrentHour(mInitialHourOfDay);
		mTimePicker.setCurrentMinute(mInitialMinute);
		mTimePicker.setOnTimeChangedListener(mOnTimeChangedListener);
		mTimePicker.setValidationCallback(mValidationCallback);
/*
		mDrawerLayoutV= (DrawerLayoutV)findViewById(R.id.drawer_layout_v);
		mDrawerLayoutV.setDrawerListener(new DrawerLayoutV.DrawerListener(){
			@Override
			public void onDrawerSlide(View drawerView, float slideOffset){
			}
			
			@Override
			public void onDrawerOpened(View drawerView){
			}
			
			@Override
			public void onDrawerClosed(View drawerView){
			}

			@Override
			public void onDrawerStateChanged(int newState){
			}
		});
*/
		mActionView = (ImageView)findViewById(R.id.wt_option_button);
/*
		mTitleView = (TextView)findViewById(R.id.wt_title_text);
		mTitleView.setText((mAlarm == null) ? R.string.add_alarm : R.string.menu_edit_alarm);
		mTitleView.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				if(mOnAlarmEditListener != null){
					mOnAlarmEditListener.onAlarmEditDone(mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute(),mRingtone,mVibrateOn,mCheckedItems);
					WtAlarmEditDialog.this.dismiss();
				}
			}
		});
*/
		View customPanel = findViewById(com.android.internal.R.id.customPanel);
		if(customPanel != null){
			customPanel.setPadding(8,0,8,0);
		}
		mOtherView = findViewById(R.id.drag_root);

		View repeatView = findViewById(R.id.edit_option_repeat);
		repeatView.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				mRepeatOn = !mRepeatOn;
				updateRepeatView();
			}
		});
		
		mRepeatCheckBox = (ImageView)repeatView.findViewById(R.id.repeat_check_box);
		
		// Build button for each day.
		mRepeatDaysView = (ViewGroup)findViewById(R.id.repeat_days);
		for (int i = 0; i < 7; i++) {
			final int buttonIndex = i;
			
			mDayButtons[i] = (Button) mLayoutInflater.inflate(R.layout.day_button, mRepeatDaysView, false /* attachToRoot */);
			mDayButtons[i].setText(mWeekDays[i]);
			if(mCheckedItems[i]){
				turnOnDayOfWeek(i);
			}else{
				turnOffDayOfWeek(i);
			}
			mRepeatDaysView.addView(mDayButtons[i]);
			mDayButtons[i].setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					toggleDayOfWeek(buttonIndex);
					updateRepeatCheckView();
				}
			});
		}
		updateRepeatView();

		View ringtoneView = findViewById(R.id.edit_option_ringtone);
		ringtoneView.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				if(mOnAlarmEditListener != null){
					mOnAlarmEditListener.onLaunchRingTonePicker();
				}
			}
		});
		mRingtoneView = (TextView)ringtoneView.findViewById(R.id.ringtone_title_view);
		updateRingtoneView();

		View vibrateView = findViewById(R.id.edit_option_vibrate);
		vibrateView.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				mVibrateOn = !mVibrateOn;
				updateVibrateView();
			}
		});
		mVibrateCheckBox = (ImageView)vibrateView.findViewById(R.id.vibrate_check_box);
		updateVibrateView();
	}

	private final OnTimeChangedListener mOnTimeChangedListener = new OnTimeChangedListener(){
		@Override
		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			/* do nothing */
		}
	};

	private final ValidationCallback mValidationCallback = new ValidationCallback() {
		@Override
		public void onValidationChanged(boolean valid) {
			//final Button positive = getButton(BUTTON_POSITIVE);
			//if (positive != null) {
				//positive.setEnabled(valid);
			//}
		}
	};

	private void updateVibrateView(){
		mVibrateCheckBox.setImageResource(mVibrateOn ? R.drawable.zzz_vibrate_on : R.drawable.zzz_vibrate_off);
	}

	private void updateRingtoneView(){
		mRingtoneView.setText(getRingtoneTitle());
	}

	private String getRingtoneTitle(){
		Context context = getContext();
		if(mRingtone == null){
			return context.getResources().getString(R.string.silent_alarm_summary);
		}
		String ringtone = null;
		if (!AlarmClockFragment.isRingtoneExisted(context, mRingtone.toString())) {
			mRingtone = RingtoneManager.getActualDefaultRingtoneUri(context,RingtoneManager.TYPE_ALARM);
			if (mRingtone == null) {
				mRingtone = Uri.parse(AlarmClockFragment.SYSTEM_SETTINGS_ALARM_ALERT);
			}
		}
		Ringtone ringTone = RingtoneManager.getRingtone(context, mRingtone);
		ringtone = ringTone.getTitle(context);
		return ringtone;
	}

	@Override
	public void onRingtoneChanged(Uri alert){
		mRingtone = alert;
		updateRingtoneView();
	}

	public interface OnAlarmEditListener {
		public void onLaunchRingTonePicker();
		public void onAlarmEditDone(int hourOfDay, int minute, Uri alert, boolean vibrateOn, boolean[] repeatDays);
	}
	private OnAlarmEditListener mOnAlarmEditListener;

	private boolean mShowFinal;
	private View.OnClickListener mOnClickListener = new View.OnClickListener(){
		public void onClick(View view){
			if(!mShowFinal){
				mShowFinal = true;
				WtAlarmEditDialog.this.getButton(DialogInterface.BUTTON_POSITIVE).setText(getContext().getText(android.R.string.yes));
				mTimePicker.setVisibility(View.GONE);
				mOtherView.setVisibility(View.VISIBLE);
			}else{
				mOnAlarmEditListener.onAlarmEditDone(mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute(),mRingtone,mVibrateOn,mCheckedItems);
				WtAlarmEditDialog.this.dismiss();
			}
		}
	};

	private DialogInterface.OnClickListener mDialogInterfaceListener = new DialogInterface.OnClickListener(){
		public void onClick(DialogInterface dialog, int which){
		}
	};

        private void turnOffDayOfWeek(int dayIndex) {
            mDayButtons[dayIndex].setActivated(false);
            mDayButtons[dayIndex].setTextColor(getContext().getResources().getColor(R.color.black));
        }

        private void turnOnDayOfWeek(int dayIndex) {
            mDayButtons[dayIndex].setActivated(true);
            mDayButtons[dayIndex].setTextColor(getContext().getResources().getColor(R.color.white));
        }

	private void toggleDayOfWeek(int dayIndex){
		if(mDayButtons[dayIndex].isActivated()){
			turnOffDayOfWeek(dayIndex);
		}else{
			turnOnDayOfWeek(dayIndex);
		}
		mCheckedItems[dayIndex] = mDayButtons[dayIndex].isActivated();
	}

	private void updateRepeatView(){
		mRepeatCheckBox.setImageResource(mRepeatOn ? R.drawable.zzz_vibrate_on : R.drawable.zzz_vibrate_off);
		mRepeatDaysView.setVisibility(mRepeatOn ? View.VISIBLE : View.GONE);
	}

	private void updateRepeatCheckView(){
		boolean repeatOn = false;
		for(int i=0;i<mCheckedItems.length;i++){
			if(mCheckedItems[i]){
				repeatOn = true;
				break;
			}
		}
		if(mRepeatOn != repeatOn){
			mRepeatOn = repeatOn;
			mRepeatCheckBox.setImageResource(mRepeatOn ? R.drawable.zzz_vibrate_on : R.drawable.zzz_vibrate_off);
		}
	}
}
